package com.lkd.handler;

import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutResultContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Topic(TopicConfig.VMS_RESULT_TOPIC)
public class VendoutResultHandler implements MsgHandler {

@Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
          //1-解析数据
        VendoutResultContract resultContract = JsonUtil.getByJson(jsonMsg, VendoutResultContract.class);
        //3-如果发货失败，恢复库存，发起订单回滚，返回发货结果
        if(Strings.isNullOrEmpty(resultContract.getInnerCode())){
                   return;
        }
        vendingMachineService.vendoutResult(resultContract);
    }
}
